﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UMLTouch.Core.Entities;
using System.Windows.Controls;
using System.Windows;
using UMLTouch.Core;
using UMLTouch.Core.UI;
using UMLTouch.Core.Models;

namespace UMLTouch.Domain.ClassDiagram.Items
{
    public class EntityItem : IDiagramItem,ISlotItem
    {

        public event EventHandler<DiagramItemMovedEventArgs> Item_Moved;

        List<AnchorSlot> _slots = new List<AnchorSlot>();

        Point _position;

        public Point Position
        {
            get { return _position; }
            set { _position = value; }
        }
       
        public IEnumerable<Segment> Segments
        {
            get;
            protected set;
        }

        public IEnumerable<System.Windows.Ink.Stroke> Strokes
        {
            get;
            protected set;
        }

        public IEnumerable<System.Windows.Point> IntersectionPoints
        {
            get;
            protected set;
        }



        public Double X
        {
            get
            {
                return _position.X;
            }
            set
            {
                Vector move = new Vector(value - _position.X, 0);
                _position.X = value;
                raiseItemMoved(move);
                

            }
        }

        public Double Y
        {
            get
            {
                return _position.Y;
            }
            set
            {
                Vector move = new Vector(0,value - _position.Y);
                _position.Y = value;
                raiseItemMoved(move);
            }
        }

        public EntityItem(Shape shape)
        {
            Segments = shape.Segments.ToList();
            Strokes = shape.BaseStrokes.ToList();
            IntersectionPoints = shape.Intersections.Select(inter => inter.IntersectionPoint).ToList();
            Position = shape.TopLeft;
           
            AnchorSlot top = new AnchorSlot(this, new Point(X+60,Y));
            _slots.Add(top);

            AnchorSlot left = new AnchorSlot(this, new Point(X, Y + 60));
            _slots.Add(left);

            AnchorSlot bottom = new AnchorSlot(this, new Point(X + 60, Y + 120));
            _slots.Add(bottom);

            AnchorSlot Right = new AnchorSlot(this, new Point(X + 120, Y + 60));
            _slots.Add(Right);

        }

        public EntityItem()
        {
            Segments = new List<Segment>();
            Strokes = null;
            IntersectionPoints = new List<Point>();
        }

        void raiseItemMoved(Vector move)
        {
            DiagramItemMovedEventArgs args = new DiagramItemMovedEventArgs(this, move);
            if (Item_Moved != null)
            {
                Item_Moved(this, args);
            }
        }





        #region ISlotItem Members

        public IEnumerable<AnchorSlot> Slots
        {
            get { return _slots; }
        }

        #endregion

       
    }
}
